package com.gzu.emr.controller;

import com.gzu.emr.dto.Diagnosis;
import com.gzu.emr.service.DiagnosisService;
import com.gzu.emr.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

import static com.gzu.emr.util.ResultGenerator.genFailResult;
import static com.gzu.emr.util.ResultGenerator.genSuccessResult;

@Controller
@RequestMapping("/diagnosis")
public class DiagnosisController {

    @Resource
    private DiagnosisService diagnosisService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "保存确诊记录")
    @Parameters({@Parameter(name = "medical_record_id", description = "病历号", required = true),
            @Parameter(name = "result", description = "诊断结果", required = true),
            @Parameter(name = "suggestion", description = "治疗建议", required = true),
            @Parameter(name = "attention", description = "注意事项", required = true),})
    public Result Login(@RequestBody Map<String, Object> params) {
        var recordId = params.get("medical_record_id");
        if (recordId == null) {
            return genFailResult("bad request body");
        }
        var result = params.get("result");
        if (recordId == null) {
            return genFailResult("bad request body");
        }
        var suggestion = params.get("suggestion");
        if (suggestion == null) {
            return genFailResult("bad request body");
        }
        var attention = params.get("attention");
        if (attention == null) {
            return genFailResult("bad request body");
        }
        Diagnosis diagnosis = new Diagnosis();
        diagnosis.setMedicalRecordId((Integer) recordId);
        diagnosis.setResult((String)result);
        diagnosis.setSuggestion((String) suggestion);
        diagnosis.setAttention((String)attention);
        diagnosis.setDate(new Date());

        if (diagnosisService.insertDiagnosis(diagnosis) == 1) {
            return genSuccessResult();
        } else {
            return genFailResult("server internal error");
        }
    }



}
